package it.polimi.traveldream.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

/**
 * The persistent class for the package database table.
 * 
 */
@Entity
@Table(name = "package")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
@NamedQuery(name = "Package.findAll", query = "SELECT p FROM Package p ")
public abstract class Package implements Serializable {
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	@NotEmpty
	private String city;

	@NotNull
	@Min(1)
	private Integer duration;

	@NotNull
	@Min(0)
	private Float price;

	@ManyToOne
	@JoinColumn(name = "accomodation", nullable = false)
	private Accomodation accomodation;

	@ManyToOne
	@JoinColumn(name = "going", nullable = false)
	private Transport going;

	@ManyToOne
	@JoinColumn(name = "back", nullable = false)
	private Transport back;

	@OneToMany(cascade = CascadeType.ALL, mappedBy = "pack")
	private List<Schedule> schedule;

	public Package() {
		this.schedule = new ArrayList<>();
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getCity() {
		return this.city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public Integer getDuration() {
		return this.duration;
	}

	public void setDuration(Integer duration) {
		this.duration = duration;
	}

	public Accomodation getAccomodation() {
		return this.accomodation;
	}

	public void setAccomodation(Accomodation accomodation) {
		this.accomodation = accomodation;
	}

	public Transport getGoing() {
		return this.going;
	}

	public void setGoing(Transport going) {
		this.going = going;
	}

	public Transport getBack() {
		return this.back;
	}

	public void setBack(Transport back) {
		this.back = back;
	}

	public List<Schedule> getSchedule() {
		return this.schedule;
	}

	public void setSchedule(List<Schedule> schedule) {
		this.schedule = schedule;
	}

	public boolean addSchedule(Schedule schedule) {
		schedule.setPack(this);
		return this.getSchedule().add(schedule);
	}

	public boolean removeSchedule(Schedule schedule) {
		return this.getSchedule().remove(schedule);
	}

	public Float getPrice() {
		return this.price;
	}

	public void setPrice(Float price) {
		this.price = price;
	}

}